package com.nymph.executor;

import com.nymph.constant.Constant;
import com.nymph.executor.parameter.DefaultParameterHandler;
import com.nymph.executor.parameter.ParameterHandler;
import com.nymph.executor.resultset.DefaultResultSetHandler;
import com.nymph.executor.resultset.ResultSetHandler;
import com.nymph.executor.statement.SimpleStatementHandler;
import com.nymph.executor.statement.StatementHandler;
import com.nymph.mapping.MappedStatement;
import com.nymph.session.Configuration;

import java.sql.*;
import java.util.List;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
public class SimpleExecutor implements Executor {

    private static Connection connection;

    private Configuration configuration;

    public SimpleExecutor(Configuration configuration) {
        this.configuration = configuration;
    }

    static {
        initConnect();
    }

    //根据参数查询数据库
    @Override
    public <E> List<E> doQuery(MappedStatement ms, Object parameter) {
        try {
            //获取连接
            Connection connection = getConnection();

            //获取mapped-statement信息
            MappedStatement mappedStatement = configuration.getMappedStatement(ms.getSqlId());

            //实例化statementHandler
            StatementHandler statementHandler = new SimpleStatementHandler(mappedStatement);

            //获取preparedStatement
            PreparedStatement preparedStatement = statementHandler.prepare(connection);

            //实例化ParameterHandler，将SQL语句中？参数化
            ParameterHandler parameterHandler = new DefaultParameterHandler(parameter);
            parameterHandler.setParameters(preparedStatement);

            //执行sql! 得到结果集resultSet
            ResultSet resultSet = statementHandler.query(preparedStatement);

            ResultSetHandler resultSetHandler = new DefaultResultSetHandler(mappedStatement);

            return resultSetHandler.handleResultSets(resultSet);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void doUpdate(MappedStatement ms, Object parameter) {
        try {
            //1.获取数据库连接
            Connection connection = getConnection();

            //2.获取MappedStatement信息，里面有sql信息
            MappedStatement mappedStatement = configuration.getMappedStatement(ms.getSqlId());

            //3.实例化StatementHandler对象，
            StatementHandler statementHandler = new SimpleStatementHandler(mappedStatement);

            //4.通过StatementHandler和connection获取PreparedStatement
            PreparedStatement preparedStatement = statementHandler.prepare(connection);

            //5.实例化ParameterHandler，将SQL语句中？参数化
            ParameterHandler parameterHandler = new DefaultParameterHandler(parameter);
            parameterHandler.setParameters(preparedStatement);

            //6.执行SQL
            statementHandler.update(preparedStatement);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        if (connection != null) {
            return connection;
        } else {
            throw new SQLException("无法连接数据库");
        }
    }

    //初始化数据库连接池
    private static void initConnect() {
        String driver = Configuration.getProperty(Constant.DB_DRIVER_CONF);
        String url = Configuration.getProperty(Constant.DB_URL_CONF);
        String username = Configuration.getProperty(Constant.DB_USERNAME_CONF);
        String password = Configuration.getProperty(Constant.DB_PASSWORD);

        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
